package com.eascs.web.o2o.scheduler.service;

import java.sql.Timestamp;
import java.util.Date;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.DigestUtils;
import com.eascs.common.util.UuidUtil;
import com.eascs.notify.constant.ModelResult;
import com.eascs.notify.sms.SmsNotifier;
import com.eascs.notify.sms.SmsNotifyRequest;
import com.eascs.notify.sms.SmsNotifyResponse;
import com.eascs.notify.sms.SmsSendType;
import com.eascs.web.o2o.scheduler.dao.O2oh5Dao;
import com.eascs.web.o2o.scheduler.entity.ApplayInfoVo;
import com.eascs.web.o2o.scheduler.util.Constant;

@Service
public class O2oh5Service {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Inject
	O2oh5Dao o2oh5Dao;
	@Reference
	SmsNotifier smsNotifier;

	public <T> void SendMessage(String sql) {
		int index = 0;
		int total = 0;
		while (index >= 0) {

			SqlPageQuery<ApplayInfoVo> sqlPageQuery = new SqlPageQuery<>(ApplayInfoVo.class,
					Constant.PROCESS_DATA_LIMIT, index, sql);
			PageResult<ApplayInfoVo> pageResult = this.o2oh5Dao.findDataWithPageByMySql(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;

			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				ApplayInfoVo obj = pageResult.getData().get(i);
				logger.debug("total:{},{}", ++total, obj.toString());
				SmsNotifyRequest re = new SmsNotifyRequest();
				re.setRequestId(DigestUtils.md5With16(UuidUtil.generateFullUuid()));
				re.setRequestDateTime(new Timestamp(new Date().getTime()));
				re.setBizCode("o2oh5营销管理申请借款信息");
				re.setSystemCode("o2oh5");
				re.setSmsSendType(SmsSendType.INSTANT);
				re.setRecipientNumber(obj.getCustomer_manager_phone());
				re.setContent("“【客户贷款申请】姓名："+obj.getUname()+"，客户电话："+obj.getTelphone()+"，申请贷款："+obj.getLoan_money()/10000+"万，所在地："+obj.getProvince()+"省"+obj.getCity()+"市"+obj.getDetail_addresss()+"，合作平台："+obj.getCooperate_platform()+"，请安排工作人员及时跟进，谢谢！”");
				re.setReceiveName(obj.getUname());
				re.setSendName("怡亚通");
				try {
                  if(StringUtils.isEmpty(obj.getCustomer_manager_phone())) return ;
					ModelResult<SmsNotifyResponse>  flag=smsNotifier.notify(re);
					 if(flag.isSuccess()){
						 o2oh5Dao.excuteSql("update applay_info set is_send=1 where id=?", obj.getId());	 
					 }
					
					
				} catch (Exception e) {
					logger.error("数据库异常", e);

				}
			}

		}

	}

}
